package symbols;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.concurrent.ConcurrentHashMap;

public class ShortcutManager extends JFrame{

	private JPanel jContentPane = null;
	private static ConcurrentHashMap<Integer,String> map = null;  //  @jve:decl-index=0:

	
	public ShortcutManager(){
		new JFrame();
		map = new ConcurrentHashMap<Integer,String>(100);
		setSize(new Dimension(746, 476));
		setContentPane(getJContentPane());
		setDefaultBinds();
	}
	

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			JLabel title = new JLabel("Symbol Mapping Tool");
			title.setBounds(5, 5, 50, 20);
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(title,BorderLayout.NORTH);
		}
		return jContentPane;
	}	
	
	/**
	 * Method to check is map contains the object with key i
	 * @param i key
	 * @return true if contains it, false otherwise
	 * @author Balazs Pete
	 */
	public static boolean containsObject(Integer i){
		if(map.containsKey(i)){
			return true;
		}
		return false;
	}
	
	/**
	 * Method to update the map with a new entry
	 * @param i keycode
	 * @param s character
	 * @author Balazs Pete
	 */
	public static void updateMap(Integer i,String s){
		if(map.get(i)!=null) {
			switch (JOptionPane.showOptionDialog(null, "The button is already assigned. Do you want to overwrite?", "Confirm Overwrite", 
					JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, JOptionPane.YES_OPTION)) {
			case JOptionPane.CLOSED_OPTION : break;
			case JOptionPane.OK_OPTION : map.put(i,s); break;
			case JOptionPane.CANCEL_OPTION : break;
			}
		} else {
			map.put(i,s);
		}
	}
	
	/**
	 * Method to remove an entry from the map with key i
	 * @param i keycode
	 */
	public static void removeBind(Integer i){
		map.remove(i);
	}
	
	/**
	 * Method to execute the insert depending on input
	 * @param x keycode
	 * @author Balazs Pete
	 */
	public void executeInsert(int x){
		String s = map.get(x);
		if(s!=null){
			main.MainWindow.insertText(s+"",main.MainWindow.getSymbolSpacesStatus());
		}
	}
	
	/**
	 * Method to set the dafault bindings
	 * @author Balazs Pete
	 */
	private void setDefaultBinds(){
		
		JPanel jp = new JPanel();
		
		ShortcutButton b1 = new ShortcutButton("\u22C0",'a');
		b1.setBindFromLabel();
		jp.add(b1);
		ShortcutButton b2 = new ShortcutButton("\u2261",' ');
		b2.setBindFromLabel();
		jp.add(b2);
		ShortcutButton b3 = new ShortcutButton("\u22C1",'d');
		b3.setBindFromLabel();
		jp.add(b3,BorderLayout.CENTER);
		ShortcutButton b4 = new ShortcutButton("\u00AC",'q');
		b4.setBindFromLabel();
		jp.add(b4,BorderLayout.CENTER);
		ShortcutButton b5 = new ShortcutButton("\u21D2",'x');
		b5.setBindFromLabel();
		jp.add(b5,BorderLayout.CENTER);
		ShortcutButton b6 = new ShortcutButton("\u23BF",'u');
		b6.setBindFromLabel();
		jp.add(b6,BorderLayout.CENTER);
		ShortcutButton b7 = new ShortcutButton("\u23CC",'i');
		b7.setBindFromLabel();
		jp.add(b7,BorderLayout.CENTER);
		ShortcutButton b8 = new ShortcutButton("\u23BE",'j');
		b8.setBindFromLabel();
		jp.add(b8,BorderLayout.CENTER);
		ShortcutButton b9 = new ShortcutButton("\u23CB",'k');
		b9.setBindFromLabel();
		jp.add(b9,BorderLayout.CENTER);
		ShortcutButton b10 = new ShortcutButton("\u2264",'n');
		b10.setBindFromLabel();
		jp.add(b10,BorderLayout.CENTER);
		ShortcutButton b11 = new ShortcutButton("\u2265",'m');
		b11.setBindFromLabel();
		jp.add(b11,BorderLayout.CENTER);
		ShortcutButton b12 = new ShortcutButton("\u2191",'y');
		b12.setBindFromLabel();
		jp.add(b12,BorderLayout.CENTER);
		ShortcutButton b13 = new ShortcutButton("\u2193",'h');
		b13.setBindFromLabel();
		jp.add(b13,BorderLayout.CENTER);
		ShortcutButton b14 = new ShortcutButton("\u2291",'b');
		b14.setBindFromLabel();
		jp.add(b14,BorderLayout.CENTER);
		ShortcutButton b15 = new ShortcutButton("\u2297",'w');
		b15.setBindFromLabel();
		jp.add(b15,BorderLayout.CENTER);
		ShortcutButton b16 = new ShortcutButton("\u2203",'g');
		b16.setBindFromLabel();
		jp.add(b16,BorderLayout.CENTER);
		ShortcutButton b17 = new ShortcutButton("\u2200",'t');
		b17.setBindFromLabel();
		jp.add(b17,BorderLayout.CENTER);
		
		jContentPane.add(jp);
	}

}
